<template>
  <el-dialog title="商品打折" :close-on-click-modal="false" :visible.sync="visible">
    <el-button class="miui-dialog-close-btn" @click="visible = false">关闭页面</el-button>
    <el-tabs v-model="activeName" type="border-card" @tab-click="tabsChange">
      <el-tab-pane label="员工打折权限" name="first">
        <el-form :model="dataForm1" ref="dataForm1" label-width="120px">
          <el-form-item label="员工工号">
            <el-input v-model="dataForm1.workNumber" @keyup.enter.native="jobNumberChange"></el-input>
          </el-form-item>
          <el-form-item label="密码">
            <el-input v-model="dataForm1.password" type="password" ref="passwordInput" @keyup.enter.native="staffLogin">
              <el-button slot="append" @click="staffLogin">登录</el-button>
            </el-input>
          </el-form-item>
        </el-form>
        <el-form label-width="120px" v-if="discountData.show">
          <el-form-item label="打折用户"> <span style="color: #409EFF;">{{discountData.discountUsername}}</span></el-form-item>
          <el-form-item label="折扣">
            <el-input v-model="discountData.rate" ref="rateInput" @keyup.enter.native="conductDiscount">
              <el-button slot="append" @click="conductDiscount">打 折</el-button>
            </el-input>
          </el-form-item>
        </el-form>
      </el-tab-pane>
      <el-tab-pane label="打折卡打折" name="second">
        <el-form :model="dataForm2" ref="dataForm2" label-width="120px">
          <el-form-item label="打折卡号">
            <el-input v-model="dataForm2.code" @keyup.enter.native="cardNumberChange"></el-input>
          </el-form-item>
          <el-form-item label="打折卡密码">
            <el-input type="password" v-model="dataForm2.password" ref="passwordInput2" @keyup.enter.native="cardDiscount">
              <el-button slot="append" @click="cardDiscount">打 折</el-button>
            </el-input>
          </el-form-item>
        </el-form>
      </el-tab-pane>
    </el-tabs>
  </el-dialog>
</template>

<script>
  import {getRightByWorkNumber,getRightByCardId} from '@/api/admin/discount'
  export default {
    data () {
      return {
        visible: false,
        activeName: 'first',
        dataForm1: {
          workNumber: '',
          password: ''
        },
        dataForm2: {
					code: '',
					password: ''
				},
        discountData: {
          rate: ''
        },
        discount: {
          staffName: '',
        },
        goodsList: [],           //  选择打折的商品列表
        tableData: []           //   销售单全部商品列表
      }
    },
    methods: {
      init (data) {
        this.visible = true
        this.activeName = 'first'
        this.goodsList = data.selectGoods
        this.tableData = data.tableData
        this.dataForm1 = {}
        this.discountData = {}
      },
      //  员工权限登录
      staffLogin() {
        if(!this.dataForm1.workNumber){
          this.$message.warning('请输入员工工号')
          return false
        }
        if(!this.dataForm1.password){
          this.$message.warning('请输入员工密码')
          return false
        }
        getRightByWorkNumber(this.dataForm1).then(result => {
          this.discountData = result.data.data
          this.discountData.show = true
          this.$nextTick(() => {
            this.$refs.rateInput.focus()
          })
        })
      },
      //  员工权限进行打折
      conductDiscount() {
        if(this.discountData.rate < this.discountData.discount){
          this.$message.warning('该员工最低打折权限为'+this.discountData.discount)
          return false
        }
        this.goodsList.forEach((item,index) => {
          item.discountRate = this.discountData.rate
          if([3,4].includes(item.ctgId)){
            for(let i = 0; i < this.tableData.length;i++){
              if(this.tableData[i].goodsId == item.goodsId && this.tableData[i].glassFlag == item.glassFlag && this.tableData[i].goodsDescription == item.goodsDescription){
                this.tableData[i].discountRate = this.discountData.rate
              }
            }
          }else{
            for(let i = 0; i < this.tableData.length;i++){
              if(this.tableData[i].goodsId == item.goodsId && this.tableData[i].goodsDescription == item.goodsDescription){
                this.tableData[i].discountRate = this.discountData.rate
              }
            }
          }

        })
        this.visible = false
        this.$emit('refreshForm',{value:  this.tableData, select: this.goodsList})
      },
      //  打折卡打折
      cardDiscount() {
        getRightByCardId(this.dataForm2).then(result => {
          let power = result.data.data.ctgIds
          let rate = result.data.data.rate
          this.goodsList.forEach((item,index) => {
            if(power.indexOf(item.ctgId) != -1){
              if([3,4].includes(item.ctgId)){
                for(let i = 0; i < this.tableData.length;i++){
                  if(this.tableData[i].goodsId == item.goodsId && this.tableData[i].glassFlag == item.glassFlag){
                    this.tableData[i].discountRate = rate
                  }
                }
              }else{
                for(let i = 0; i < this.tableData.length;i++){
                  if(this.tableData[i].goodsId == item.goodsId){
                    this.tableData[i].discountRate = rate
                  }
                }
              }
            }else{

            }
          })
        }).then(() => {
          this.visible = false
          this.$emit('refreshForm',{value:  this.tableData, select: this.goodsList})
        })
      },
      tabsChange(tab,event){
        if(tab.name == 'first'){
          this.dataForm1 = {}
          this.discountData = {}
        }else{
          this.dataForm2 = {}
        }
      },
      //  工号回车切换到密码输入框
      jobNumberChange() {
        this.$refs.passwordInput.focus()
      },
      cardNumberChange() {
        this.$refs.passwordInput2.focus()
      }
    }
  }
</script>

<style>
</style>
